package test.data_structure.stack;

import test.data_structure.array.DynamicArray;

public class MyArrayStack<T> implements Stack<T> {

    private DynamicArray<T> data;

    public MyArrayStack(int capacity) {
        data = new DynamicArray<>(capacity);
    }

    public MyArrayStack() {
        data = new DynamicArray<>();
    }

    @Override
    public int getSize() {
        return data.getSize();
    }

    @Override
    public boolean isEmpty() {
        return data.isEmpty();
    }

    public int getCapacity() {
        return data.getCapacity();
    }

    @Override
    public void push(T data) {
        this.data.addLast(data);
    }

    @Override
    public T pop() {
        return this.data.removeLast();
    }

    @Override
    public T peek() {
        return this.data.get(this.data.getSize() - 1);
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("Stack:");
        stringBuilder.append("[");
        for (int i = 0 ; i < this.data.getSize() ; i ++) {
            stringBuilder.append(data.get(i));
            if (i != data.getSize() - 1) {
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
